package ch.app.controller.pageHelper;

import ch.app.dao.address.AddressDao;
import ch.app.entity.db.Address;
import ch.app.service.impl.AddressServiceImpl;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Yin Yong Gang
 *         Date:
 *         Description:
 */

@Controller
public class PageHelperController {


    @Autowired
    private AddressDao addressDao;

    @Autowired
    private AddressServiceImpl addressServiceImpl;

    @RequestMapping("/ph")
    public void test2() {
        PageHelper.startPage(1, 5);
        List<Address> list = addressDao.listAddress();
        PageInfo<Address> page = new PageInfo<Address>(list);
        System.out.println("总数量：" + page.getTotal());
        System.out.println("当前页查询记录：" + page.getList().size());
        System.out.println("当前页码：" + page.getPageNum());
        System.out.println("每页显示数量：" + page.getPageSize());
        System.out.println("总页：" + page.getPages());
    }

    @GetMapping("/load")
    public String getAllPerson(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        List<Address> list = addressServiceImpl.listAddress(pageNum);
        PageInfo<Address> pageInfo = new PageInfo<Address>(list);
        model.addAttribute("pageInfo", pageInfo);
        System.out.println(JSON.toJSONString(pageInfo));
        return "pageHelper";
    }
}
